package com.llxy.web.controller.worker;
import com.llxy.common.annotation.Log;
import com.llxy.common.core.controller.BaseController;
import com.llxy.common.core.domain.AjaxResult;
import com.llxy.common.core.domain.entity.SysRole;
import com.llxy.common.core.domain.entity.SysUser;
import com.llxy.common.core.domain.model.LoginUser;
import com.llxy.common.core.page.TableDataInfo;
import com.llxy.common.enums.BusinessType;
import com.llxy.system.service.ISysRoleService;
import com.llxy.worker.domain.Homemaking;
import com.llxy.worker.domain.HomemakingJob;
import com.llxy.worker.domain.SupportJob;
import com.llxy.worker.domain.SupportWorker;
import com.llxy.worker.dto.SupportWorkerDTO;
import com.llxy.worker.service.HomemakingJobService;
import com.llxy.worker.service.SupportJobService;
import com.llxy.worker.service.SupportWorkerService;
import com.llxy.worker.service.impl.HomemakingJobServiceImpl;
import io.jsonwebtoken.lang.Collections;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
* (t_homemaking_job)表控制层
*
* @author 马瑞龙
*/
@RestController
@RequestMapping("/worker/job")
public class HomemakingJobController extends BaseController {
    /**
    * 服务对象
    */
    @Autowired
    private HomemakingJobService homemakingJobService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private SupportJobService supportJobService;
    @Autowired
    private SupportWorkerService supportWorkerService;

    /**
     * 获取岗位列表
     */
    @PreAuthorize("@ss.hasPermi('worker:job:list')")
    @GetMapping("/list")
    public TableDataInfo list(HomemakingJob homemakingJob)
    {
        startPage();
        List<HomemakingJob> homemakingJobList = homemakingJobService.selectHomemakingJobList(homemakingJob);
        return getDataTable(homemakingJobList);
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@ss.hasPermi('worker:job:add')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody HomemakingJob homemakingJob)
    {
        homemakingJob.setCreateBy(getUsername());
        return toAjax(homemakingJobService.insertSelective(homemakingJob));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@ss.hasPermi('worker:job:edit')")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody HomemakingJob homemakingJob)
    {
        return toAjax(homemakingJobService.updateByPrimaryKeySelective(homemakingJob));
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('worker:job:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Integer id)
    {
        return success(homemakingJobService.selectByPrimaryKey(id));
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@ss.hasPermi('worker:job:remove')")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Integer id)
    {
        return toAjax(homemakingJobService.deleteByPrimaryKey(id));
    }

    /**
     * 岗位投递
     */
    @PreAuthorize("@ss.hasPermi('worker:job:deliver')")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping("/deliver/{id}")
    public AjaxResult deliver(@PathVariable Integer id)
    {
        List<SysRole> roles = getLoginUser().getUser().getRoles();
        for (SysRole sysRole : roles){
            if (!"supportWorker".equals(sysRole.getRoleKey())){
                return error("护工才能投递岗位！");
            }
        }
        SupportWorker supportWorker = supportWorkerService.selectByUserId(getUserId());
        if (null == supportWorker) {
            return error("您还不是护工，请先申请成为护工！");
        }

        if (null == supportWorker.getTrainId()) {
            return error("您还未接受培训，请联系培训机构培训，并获取证书！");
        }

        Integer count = supportJobService.selectBySupportIdCount(getUserId());
        if (count >= 1){
            return error("您目前属于在岗状态!");
        }
        return toAjax(supportJobService.deliver(id,getUserId().intValue()));
    }


    @GetMapping("/count")
    public AjaxResult count()
    {
        return success(supportJobService.selectCount(getUserId()));
    }

    @GetMapping("/jobCount")
    public AjaxResult jobCount()
    {
        return success(supportJobService.selectJobCount());
    }

    /**
     * 历史岗位投递列表
     */
    @PreAuthorize("@ss.hasPermi('worker:job:deliver')")
    @GetMapping("/deliver/history")
    public AjaxResult deliverHistory()
    {
        return success(supportJobService.deliverHistory(getUserId()));
    }


}
